package com.enjia.common.framework;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.enjia.enjiashop.R;

/**
 * 包含多个Fragment的基础Activity
 * Created by 陈飞飞 on 2017/10/23.
 */
public abstract class BaseFragmentActivity extends BaseActivity {

    protected FragmentManager mFragmentManger;
    //默认展示Fragment
    private BaseFragment mDefaultFragment;
    //当前展示Fragment
    private BaseFragment curFragment;

    //当前Fragment标识
    private final String CURRENTFRAGMENTTAG = "currentFragment";
    private int defaultCommitId;

    private long lastClickTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("webView","启动时间" + System.currentTimeMillis());
        mFragmentManger = this.getSupportFragmentManager();
        super.onCreate(savedInstanceState);
    }

    /**
     * 返回到主页面
     */
    public void jumpToHome(){
        int count=mFragmentManger.getBackStackEntryCount();
        if(count>1){
            mFragmentManger.popBackStackImmediate();
            count = mFragmentManger.getBackStackEntryCount();
        }
    }
    /**
     * 外部得到FragmentManager对象
     */
    public FragmentManager getOnlymFragmentManger(){
        return mFragmentManger;
    }

    /**
     * 返回操作
     */
    public void jumpToBack(){
//        mFragmentManger.popBackStackImmediate();
        int count=mFragmentManger.getBackStackEntryCount();
        if(count == 0){
            this.finish();
        }
    }
    /**
     * 得到当前Fragment
     */
    public BaseFragment getCurrentFragment(){
        return curFragment;
    }

    /**
     * 返回操作
     */
    public void goBack() {
        //Log.e("回退事件","回退");
        int count = mFragmentManger.getBackStackEntryCount();
        BaseFragment fragment = (BaseFragment) mFragmentManger
                .findFragmentById(R.id.baseActityContent);
        if (fragment != null) {
            fragment.onBack();
        }
        if (count >= 1) {
            mFragmentManger.popBackStackImmediate();
        } else {
            finish();
        }

    }
    public FragmentNavInterface getFragmentNavInterface() {

        return new FragmentNavInterface() {
            @Override
            public void goBack() {
                BaseFragmentActivity.this.goBack();
            }

            @Override
            public void replaceFragment(BaseFragment fragment) {
                BaseFragmentActivity.this.jumpToFragment(fragment);
            }

            @Override
            public void replaceFragment(BaseFragment fragment, Boolean replace) {
                BaseFragmentActivity.this.jumpToFragment(fragment,replace);
            }
        };
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    /***
     * Fragment的跳转方法
     */
    public void jumpToFragment(BaseFragment fragment){
        //// TODO: 2017-08-15
        jumpToFragment(fragment,false);
    }

    /**
     * 添加fragment跳转方法
     *
     * @param fragment
     */
    public void jumpToFragment(BaseFragment fragment, boolean isReplace) {
        jumpToFragment(R.id.baseActityContent, fragment, isReplace);
    }

    /**
     * 添加fragment跳转方法
     *
     * @param fragment
     */
    public void jumpToFragment(int id, BaseFragment fragment, boolean isReplace) {
        // 替代当前fragment时，不加入回退栈
        if (isReplace) {
            jumpToFragment(false, id, fragment, isReplace);
        } else {
            jumpToFragment(true, id, fragment, isReplace);
        }

    }

    /**
     * 添加fragment内部分内容的替换
     *
     * @param isAddToBackStack
     *            是否加入回退栈
     * @param id
     * @param fragment
     * @param replace
     *            是否是替换
     */
    public void jumpToFragment(Boolean isAddToBackStack, int id,
                               BaseFragment fragment, boolean replace) {
        //LogUtils.e("tag","当前fragment=============="+curFragment.getClass().toString());
        // 防止重复点击
        if ((System.currentTimeMillis() - lastClickTime) < 300) {
            return;
        }
        lastClickTime = System.currentTimeMillis();
        FragmentTransaction ft = mFragmentManger.beginTransaction();
        if (replace) {
            ft.replace(id, fragment, CURRENTFRAGMENTTAG);
        } else {
            ft.add(id, fragment, CURRENTFRAGMENTTAG);
        }
        ft.show(fragment);
        if (isAddToBackStack) {
            ft.addToBackStack("");
        }
        ft.commitAllowingStateLoss();
        curFragment = fragment;
    }



}